/*
 * @Author: your name
 * @Date: 2021-01-06 11:38:26
 * @LastEditTime: 2021-01-23 23:22:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \express-seed\routes\updataImg.JS
 */
const express = require('express');
const router = express.Router();
const Result = require('../../models/result.js');
const upload = require('../../utils/multer')
const { getSellerService, addCodeService, delSellerService, updateSellerService } = require('../../service/sellCodeService')
//获取销售人员列表
router.post('/', async (req, res, next) => {
    const { page_id } = req.body
    const { status, results } = await getSellerService(page_id)
    if (status) {
        new Result(results, '查询成功').success(res)
    } else {
        new Result({}, '查询失败').fail(res)
    }
})
//添加销售二维码及信息
router.post('/add_sell', upload.array('image'), async (req, res, next) => {
    console.log('req.files :>> ', req.files);
    console.log('req.body :>> ', req.body);
    const {
        ad_person_name,
        is_start,
        page_id
    } = req.body
    const images = req.files
    if (!ad_person_name || !is_start || !images.length) return
    let imgsInfo = [] //图片信息数组
    images.forEach((file) => {
        let obj = {}
        obj.name = file.filename
        obj.imgPath = `public/img/${obj.name}`
        obj.path = file.path
        imgsInfo.push(obj)
    })
    const codeInfo = {//添加二维码的信息
        imgsInfo,  //图片信息数组
        ...req.body,
    }
    const { status } = await addCodeService(codeInfo)
    if (status) {
        new Result(codeInfo, '添加成功').success(res)
    } else {
        new Result({}, '添加失败').fail(res)
    }
    // fs.unlinkSync('./uploads/' + filename); 调用fs模块的unlinkSync方法，传入文件路径，直接删除。也可以用fs.unlink(callback)这个异步删除。
    /*  const ress = await addImgService()
     new Result(ress, '添加成功').success(res) */
})

//根据id删除销售人员
router.post('/delete_img', async (req, res, next) => {
    const { img_id, img_name } = req.body
    const ress = await delSellerService(img_id, img_name)
    if (ress) {
        new Result({ img_id }, '删除成功').success(res)
    } else {
        new Result({}, '删除失败').fail(res)
    }
})

//更新销售信息
router.post('/update_seller', async (req, res, next) => {
    const ress = await updateSellerService(req.body)
    if (ress) {
        new Result(req.body, '更新成功').success(res)
    } else {
        new Result({}, '更新失败').fail(res)
    }
})

module.exports = router;

